const { remove, acquire, cache } = require("../../common/utils")
const { debuger } = require("../../common/debuger")
const { createCoward } = require("../object/coward")
const { template } = require("./template")
const { plugin } = require("../../common/utils")

const labels = [
    {type: "match_cache", template: "common/rocker", bounds: []}
]

const match = (context, base, template) => {
    return matcher.match(context, base + "/" + template, (context, data) => {
        let keyboard = acquire("keyboard")
        if (!keyboard) {
            keyboard = {}
        }
        keyboard[template] = data
        cache("keyboard", keyboard)
    })
}


const submit = () => {
    sleep(500)
    let capture = captureScreen()
    let bitmap = capture.getBitmap()
    for (let name in template) {
        let result = plugin.match(bitmap, [name], template[name].bounds)
        if (result == undefined) {
            toast("技能【" + template[name].label + "】未发现!")
            continue
        }
        let keyboard = acquire("keyboard")
        if (!keyboard) {
            keyboard = {}
        }
        let key = name.substring(name.lastIndexOf('/') + 1)
        keyboard[key] = result
        cache("keyboard", keyboard)
        debuger.add(result)
    }
    setTimeout(function(){
        debuger.refresh()
    }, 5000);
    let coward = createCoward({
        skill: {
            1: {
                label: "普通攻击",
                cooling: 0,
                press: 200,
                cast: 0
            },
            2: {
                label: "十字斩",
                cooling: 4000,
                press: 50,
                cast: 100
            },
            3: {
                label: "崩山击",
                cooling: 5000,
                press: 0,  
                cast: 300
            },
            4: {
                label: "愤怒狂刃",
                cooling: 20000,
                press: 0,
                cast: 1000
            },
            5: {
                label: "捕梦之手",
                cooling: 9100,
                press: 0,
                cast: 200
            },
            6: {
                label: "怒气爆发",
                cooling: 20000,
                press: 0,
                cast: 200
            },
            7: {
                label: "绝念除尘击",
                cooling: 20000,
                press: 5000,
                cast: 600
            },
            8: {
                label: "爆发之刃",
                cooling: 20000,
                press: 0,
                cast: 400
            },
            9: {
                label: "暴走",
                cooling: 5000,
                press: 0,
                cast: 50
            },
            10: {
                label: "愤怒",
                cooling: 60000,
                press: 300,
                cast: 50
            },
            11: {
                label: "崩山裂地",
                cooling: 40000,
                press: 0,
                cast: 600
            },
            12: {
                label: "暗狱魔刹",
                cooling: 145000,
                press: 0,
                cast: 200
            }
        }
    })
}
exports.init = () => {
    // let role_list = cache("role_list")
    // if (!role_list || role_list.length == 0) {
    //     confirm("请前往设置页面，添加搬砖角色列表！")
    //     return
    // }
    if(!confirm("即将开始键位初始化，请前往【地下修炼场】")) {
        return false
    }
    debuger.refresh()
    remove()
    submit()
    return false
}